//package com.benmei.ntlive.server.netty.wss;
//
//import com.benmei.ntlive.server.netty.websocket.WebSocketServerHandler;
//import io.netty.channel.ChannelInitializer;
//import io.netty.channel.ChannelPipeline;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.handler.codec.http.HttpObjectAggregator;
//import io.netty.handler.codec.http.HttpServerCodec;
//import io.netty.handler.ssl.SslHandler;
//import io.netty.handler.stream.ChunkedWriteHandler;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import javax.net.ssl.KeyManagerFactory;
//import javax.net.ssl.SSLContext;
//import javax.net.ssl.SSLEngine;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.InputStream;
//import java.security.KeyStore;
//
///**
// * Create by chenhong on 2018/11/7 17:02
// * Description :netty 初始化类
// */
//@Component("sslWebSocketServerChannelInitializer")
//public class WebSocketServerChannelInitializer extends ChannelInitializer<SocketChannel> {
//    private boolean useSsl=true;
//    @Value("${websocket.ssl.type}")
//    private String sslType;
//
//    @Value("${webscoket.ssl.path}")
//    private File sslPath;
//
//    private String path;
//
//    @Value("${websocket.ssl.password}")
//    private String sslPassword;
//
//    @Resource(name = "webSocketServerHandler")
//    private WebSocketServerHandler webSocketServerHandler;
//
//    @Override
//    protected void initChannel(SocketChannel socketChannel) throws Exception {
//        //通道
//        ChannelPipeline ch = socketChannel.pipeline();
//        //useSsl表示是否开启wss模式
//        if (useSsl) {
//            String path=sslPath.getAbsolutePath();
//            SSLContext sslContext = createSSLContext(sslType, sslPath.getAbsolutePath(), sslPassword);
//            SSLEngine engine = sslContext.createSSLEngine();
//            //服务器模式
//            engine.setUseClientMode(false);
//            //认证
//            engine.setNeedClientAuth(false);
//            engine.setWantClientAuth(false);
//            //TLSv1.2包含了SSL
//            engine.setEnabledProtocols(new String[]{"TLSv1.2"});
//            ch.addLast("sslHandler", new SslHandler(engine));
//        }
//        ch.addLast("http-codec", new HttpServerCodec())  //http消息编码
//                .addLast("aggregator", new HttpObjectAggregator(65535)) //http消息组装
//                .addLast("http-chunked", new ChunkedWriteHandler())  //websocket通信支持
//                .addLast("handler", webSocketServerHandler);        //消息处理
//
//    }
//    //创建SSLContext
//    private SSLContext createSSLContext(String type, String path, String password) {
//        SSLContext sslContext=null;
//        try {
//            KeyStore ks=KeyStore.getInstance(type);
//            InputStream ksInputStream =new FileInputStream(path);
//            ks.load(ksInputStream,password.toCharArray());
//            // 基于密钥内容源的密钥管理器的工厂
//            KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
//            keyManagerFactory.init(ks,password.toCharArray());
//            sslContext=SSLContext.getInstance("TLS");
//            sslContext.init(keyManagerFactory.getKeyManagers(),null,null);
//        }catch (Exception e){
//            System.out.println("create ssl error");
//            e.printStackTrace();
//        }
//        return sslContext;
//    }
//}
